home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000043_icon-group-sender _Thu Feb 25 12:32:44 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  7KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id MAA12131
  4.     for icon-group-addresses; Thu, 25 Feb 1999 12:30:44 -0700 (MST)
  5. Message-Id: <199902251930.MAA12131@baskerville.CS.Arizona.EDU>
  6. From: "Frank Lhota" <lhotaf@lexma.meitech.com>
  7. To: <icon-group@optima.CS.Arizona.EDU>
  8. Subject: Designing the Bridge between Icon and C
  9. Date: Thu, 25 Feb 1999 13:04:29 -0500
  10. X-Priority: 3
  11. X-MSMail-Priority: Normal
  12. X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3
  13. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  14. Status: RO
  15.  
  16. As I mentioned in a previous posting, I would like to develop an Icon
  17. facility for calling a native C function in a dynamic library, that is, a C
  18. function that is not an Icon "wrapper" function. I believe this can be done
  19. in reasonably flexible and portable way. In my proposed design, a C function
  20. is loaded into Icon using the predefined function loadfunc, which takes the
  21. form
  22.  
  23.     loadfunc ( modname, funcname, conv )
  24.  
  25. where
  26.  
  27. - modname is the name of the dynamic library file,
  28. - funcname is the name of the function as it appears in the library; and
  29. - conv is a string indicating the calling convention ("C" or "Pascal"); the
  30. conv parameter defaults to "C".
  31.  
  32. The loaded external C function is called using the callout function, invoked
  33. as follows:
  34.  
  35.     callout ( funcname, retval, argv[] )
  36.  
  37. where
  38.  
  39. - funcname is the name of the external function;
  40. - retval is a prototype for the return value (defaults to &null, indicating
  41. that the C function return type is void); and
  42. - argv[] is the list of function arguments, which are to be converted to C
  43. format before the procedure is called.
  44.  
  45. To free the resources associated with loaded C function, call the Icon
  46. function unloadfunc, called with the name of the loaded function:
  47.  
  48.     unloadfunc(funcname)
  49.  
  50. The real sticking point is how to design the Icon specification of C data.
  51. Icon is relatively free of the data representation concerns that pervade C.
  52. Part of what we need to do when we to convert Icon data to C is to add a
  53. specification on how this data is to be represented.
  54.  
  55. All C types fall into two categories: basic types and derived types. The
  56. basic data types of C can be divided into three categories:
  57.  
  58. 1.    The void type;
  59. 2.    The integer types, including char, short, int, and long (as well as
  60. long long on some systems), along with their unsigned versions; and
  61. 3.    The floating point types float, double, and on some systems, long
  62. double.
  63.  
  64. Since enumerations always have integral values, we can include enumeration
  65. types with the integer types.
  66.  
  67. Representing the C void is easy. The Icon null type is the perfect
  68. counterpart to the C void type.
  69.  
  70. Integers and floats are more problematic. Icon is not concerned with number
  71. sizes, and Icon integers always have a sign. An Icon real can be directly be
  72. translated into a C double. An Icon integer can also be used to represent a
  73. signed C int. To translate Icon to other C numeric types, we need some way
  74. to specify the number format. To do this, I propose a standard Icon include
  75. file that defines the following Icon record types:
  76.  
  77.     record int ( value, size, unsigned )
  78.     record float ( value, size )
  79.  
  80. When records of these types are used as callout parameters, they have the
  81. following meanings:
  82.  
  83. - A record of the form int ( value, size, unsigned ) represents a C integer
  84. with a value given by the value component and with a length of size bytes.
  85. This C integer is unsigned if the unsigned component of the record is
  86. non-null.
  87.  
  88. - A record of the form float ( value, size ) represents a C real number with
  89. a value given by the value component of length of size bytes.
  90.  
  91. These numeric formatting records should suffice for specifying any of the
  92. basic C numeric types.
  93.  
  94. Generating the C derived types from Icon is a bit more challenging. Starting
  95. with the basic C types, all C types can be derived using the following
  96. constructs:
  97.  
  98. 1.    An array of objects of a given type;
  99. 2.    A structure containing a sequence of objects of various types;
  100. 3.    A union that is capable of containing any one of several objects of
  101. various types;
  102. 4.    A pointer to an object of a given type; or
  103. 5.    A pointer to a function.
  104.  
  105. The Icon representation of a C array or structure is relatively
  106. straightforward. From a certain point of view, an array is quite similar to
  107. a structure. Both an array and a structure are a sequence of values; the
  108. difference is that in an array, the values are all of the same type. A C
  109. array or structure can be modeled in Icon using an Icon list. When callout
  110. translates an Icon list to C, it will create a C region consisting of a
  111. sequence of the C translations of the elements in the Icon list. The int and
  112. float record types can be used to specify structure member sizes.
  113.  
  114. One of the most popular C arrays are, of course, arrays of characters. We
  115. should be able to use Icon strings to represent C strings.
  116.  
  117. Representing a C union in Icon is trickier, since there is no obvious Icon
  118. equivalent. Some important API functions use unions, so we should come up
  119. with some way of representing them in Icon. For now, I'm stumpted on this
  120. one. I would greatly appreciate anyone's input on this matter.
  121.  
  122. Pointers are very important. This is how C implements "call by reference".
  123. For many C functions, the most important outputs are returned by pointers.
  124. Icon technically uses only "call by value", but the effect of "call by
  125. reference" can be obtained by using Icon structures. We can therefore get
  126. the desired effect using the following record types:
  127.  
  128.     record Pointer ( value )
  129.     record CPointer ( value )
  130.  
  131. In the context of callout, a Pointer record represents a pointer to the C
  132. representation of the value component. When callout encounters a record of
  133. this type, it will allocate a C region to store the C translation of the
  134. Icon value. The pointer to this C region can then be used in the call to the
  135. C function. After the C function is called, the region value can then be
  136. copied back to the Pointer record, and the pointer is deallocated. For
  137. example,
  138.  
  139.     # function bar declared as
  140.     #     void bar ( int *ip );
  141.     callout ( "bar", &null, ip := Pointer ( 0 ) )
  142.     # ip.value updated by callout
  143.  
  144. The CPointer record is similar to pointer, except that it is used to
  145. indicate a pointer to a region that will not be modified by the C function.
  146. The callout function processes a CPointer record in the same way, except
  147. that the region value is not copied back to pointer value.
  148.  
  149. For now, I'm punting on coming up with an Icon representation of C function
  150. pointers. Ideally, it would be nice to be able to automatically generate C
  151. wrapper functions that call an Icon procedure, but that is a project for
  152. another day.
  153.  
  154. I've started work on this interface design, but before I take it further, I
  155. would greatly some feedback from the Icon programmer community. In
  156. particular,
  157.  
  158. - Does this design meet your interface needs?
  159. - Is there a simpler and/or more intuitive design for this interface?
  160. - How would you implement C unions in Icon?
  161.  
  162.